#####################################
###                               ###
###   Women and Party Building    ###
###                               ###
###     code_wrangling_RDD.R      ###
###                               ###
#####################################

# This script builds the datasets for the regression discontinuity design (RDD) analyses

rm( list=ls() )
library(stringr)

cat("Building the datasets for the RDD analyses...\n")


# This line uses the rstudioapi package to set the working directory to the same folder where this script is saved.
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))

# Alternatively, you can use setwd( PATH ) to set this directory manually


### Step 1: Code Dependent Variables (and other variables that rely on the party membership dataset)

# Load the aggregated party-municipality-term level dataset created in code_wrangling_MemberGender.R
data <- readRDS("../1_data/cleaned/data_cleaned_membership_PartyMunicipalityTerm.rds")


# Our main DV is the Gender Gap in membership recruitment, which is the 
# difference between the female recruitment rate and the male recruitment rate
# (0 means that women and men joined the party at the same rate)
data$Gender.Gap <- data$Male.Recruit.Share - data$Female.Recruit.Share

# Attrition rates
data$Switch.Rate.Female <- data$Switched.Female / data$Female.Recruits
data$Switch.Rate.Male <- data$Switched.Male / data$Male.Recruits
data$Disaffiliation.Rate.Female <- data$Left.Female / data$Female.Recruits
data$Disaffiliation.Rate.Male <- data$Left.Male / data$Male.Recruits
data$Attrition.Rate.Female <- data$Switch.Rate.Female + data$Disaffiliation.Rate.Female
data$Attrition.Rate.Male <- data$Switch.Rate.Male + data$Disaffiliation.Rate.Male
data$Attrition.Rate <- (data$Switched.Female + data$Switched.Male + data$Left.Female + data$Left.Male) / (data$Female.Recruits + data$Male.Recruits)

var.names <- c("Recruit.Share", "Female.Recruit.Share", "Male.Recruit.Share", "Gender.Gap", "Attrition.Rate" )
data[, var.names][is.na(data[, var.names])] <- 0

data$Year <- data$PERIODO

# Lagged DVs (for placebo test in Appendix FIGURE D.1 and TABLE D.1)
lagged.data <- data
lagged.data$PERIODO <- lagged.data$PERIODO + 4
lagged.data <- lagged.data[,c("PERIODO", "MunCode", "Party", "Gender.Gap", "Recruit.Share", "Attrition.Rate")]
var.names <- c("Gender.Gap", "Recruit.Share", "Attrition.Rate")
names(lagged.data) <- c("PERIODO", "MunCode", "Party", paste(var.names, ".Lagged", sep=""))
data <- merge(data, lagged.data, by=c("PERIODO", "MunCode", "Party"), all.x=T)

# Add data on cumulative recruitment by gender, across all parties
recruits <- aggregate(cbind(Male.Recruits, Female.Recruits) ~ MunCode + PERIODO, FUN="sum", data=data)
d04 <- aggregate(cbind(Male.Recruits, Female.Recruits) ~ MunCode, FUN="sum", data=recruits[recruits$PERIODO < 2004,])
d08 <- aggregate(cbind(Male.Recruits, Female.Recruits) ~ MunCode, FUN="sum", data=recruits[recruits$PERIODO < 2008,])
d12 <- aggregate(cbind(Male.Recruits, Female.Recruits) ~ MunCode, FUN="sum", data=recruits[recruits$PERIODO < 2012,])
d16 <- aggregate(cbind(Male.Recruits, Female.Recruits) ~ MunCode, FUN="sum", data=recruits[recruits$PERIODO < 2016,])
d04$Year <- 2004
d08$Year <- 2008
d12$Year <- 2012
d16$Year <- 2016
cumulative.recruits <- rbind(d04, d08, d12, d16)
names(cumulative.recruits)[2:3] <- c("Cumulative.Male.Recruits", "Cumulative.Female.Recruits")

lagged.recruits <- recruits
lagged.recruits$Year <- lagged.recruits$PERIODO + 4
lagged.recruits$Lagged.Male.Recruits <- lagged.recruits$Male.Recruits
lagged.recruits$Lagged.Female.Recruits <- lagged.recruits$Female.Recruits
lagged.recruits <- lagged.recruits[lagged.recruits$Year %in% c(2004, 2008, 2012, 2016), c("MunCode", "Year", "Lagged.Male.Recruits", "Lagged.Female.Recruits")]

electorate <- unique(data[, c("PERIODO", "MunCode", "Female.Voters", "Male.Voters")])
electorate$Year <- electorate$PERIODO

past.recruits.data <- merge(cumulative.recruits, lagged.recruits, by=c("MunCode", "Year"), all.x=T)
past.recruits.data <- merge(past.recruits.data, electorate[electorate$Year %in% c(2004, 2008, 2012, 2016),], by=c("MunCode", "Year"), all=T)
past.recruits.data$Cumulative.Recruits.Share <- 1000*(past.recruits.data$Cumulative.Male.Recruits + past.recruits.data$Cumulative.Female.Recruits) / (past.recruits.data$Female.Voters + past.recruits.data$Male.Voters)
past.recruits.data$Cumulative.Female.Recruits.Share <- 1000*(past.recruits.data$Cumulative.Female.Recruits) / (past.recruits.data$Female.Voters)
past.recruits.data$Cumulative.Male.Recruits.Share <- 1000*(past.recruits.data$Cumulative.Male.Recruits) / (past.recruits.data$Male.Voters)
past.recruits.data$Lagged.Recruits.Share <- 1000*(past.recruits.data$Lagged.Male.Recruits + past.recruits.data$Lagged.Female.Recruits) / (past.recruits.data$Female.Voters + past.recruits.data$Male.Voters)
past.recruits.data$Lagged.Female.Recruits.Share <- 1000*(past.recruits.data$Lagged.Female.Recruits) / (past.recruits.data$Female.Voters)
past.recruits.data$Lagged.Male.Recruits.Share <- 1000*(past.recruits.data$Lagged.Male.Recruits) / (past.recruits.data$Male.Voters)

past.recruits.data <- past.recruits.data[, c("MunCode", "Year", "Cumulative.Recruits.Share", "Cumulative.Female.Recruits.Share", "Cumulative.Male.Recruits.Share", "Lagged.Recruits.Share", "Lagged.Female.Recruits.Share", "Lagged.Male.Recruits.Share")]
past.recruits.data$ANO_ELEICAO <- past.recruits.data$Year

data <- merge(data, past.recruits.data[, c("MunCode", "Year", "Cumulative.Female.Recruits.Share", "Lagged.Female.Recruits.Share")], by=c("MunCode", "Year"), all.x=T)


# Add in municipal-level data (this will be used for the placebo tests in Appendix Figure D.1)
mun.data <- readRDS("../1_data/raw/data_raw_Municipality.rds")
data <- merge(data, mun.data, by="MunCode", all.x=T)


# Add lagged election data (this will be used for the placebo tests in Appendix Figure D.1)
federal <- readRDS("../1_data/raw/data_raw_FederalDeputiesElections.rds")
federal$MunCode <- as.numeric(federal$MunCode)
pt <- federal[federal$SIGLA_PARTIDO == "PT",]
pt$PT.Deputies.Voteshare <- pt$Deputies.Voteshare
pt$Year <- pt$ANO_ELEICAO + 2
data <- merge(data, pt[, c("PT.Deputies.Voteshare", "Year", "MunCode")], by=c("Year", "MunCode"), all.x=T)


# Add annual recruitment data for election-cycle analysis (this will be used for Figure Q.2)
month.data <- readRDS("../1_data/cleaned/data_cleaned_membership_PartyMunicipalityMonth.rds")
year.data <- month.data[, c("MunCode", "PERIODO", "Party", "G.1", "G.2", "G.3", "G.4")]
names(year.data) <- c("MunCode", "PERIODO", "Party", "Gender.Gap.Year1", "Gender.Gap.Year2", "Gender.Gap.Year3", "Gender.Gap.Year4")
data <- merge(data, year.data, by=c("MunCode", "PERIODO", "Party"), all.x=T)


# Code ratio of female to male voters in the municipality
data$Voter.Gender.Ratio <- data$Female.Voters / data$Male.Voters
data$Voter.Gender.Ratio <- data$Female.Voters / data$Male.Voters


# Add data on the candidate pool in the next election (this will be used in Appendix TABLE P.1)
candidates <- readRDS("../1_data/cleaned/data_cleaned_Candidates.rds")
vars <- c("MunCode", "ANO_ELEICAO", "Woman",  "SIGLA_PARTIDO")
council.candidates <- candidates[candidates$DESCRICAO_CARGO == "VEREADOR", vars]
council.winners <- candidates[candidates$DESCRICAO_CARGO == "VEREADOR" & candidates$Winner, vars]
# Add in data on the 2020 candidates
data2020 <- readRDS("../1_data/raw/data_raw_2020candidates.rds")
data2020$MunCode <- as.numeric(data2020$SG_UE)
data2020$SIGLA_PARTIDO <- data2020$SG_PARTIDO
data2020$Winner <- data2020$DS_SIT_TOT_TURNO %in% c("ELEITO", "ELEITO POR MÉDIA", "ELEITO POR QP")
data2020$Woman <- data2020$DS_GENERO == "FEMININO"
council.candidates <- rbind(council.candidates, data2020[data2020$DS_CARGO == "VEREADOR",vars])
council.winners <- rbind(council.winners, data2020[data2020$DS_CARGO == "VEREADOR" & data2020$Winner,vars])
council.candidates$Party <- council.candidates$SIGLA_PARTIDO
council.winners$Party <- council.winners$SIGLA_PARTIDO
# Aggregate
councilors.gendershare.total <- aggregate(Woman ~ ANO_ELEICAO + MunCode, FUN="mean", data=council.winners)
candidates.gendershare.total <- aggregate(Woman ~ ANO_ELEICAO + MunCode, FUN="mean", data=council.candidates)
# Adjust the dates so that they are matched to the previous municipal election
councilors.gendershare.total$Year <- councilors.gendershare.total$ANO_ELEICAO - 4
councilors.gendershare.total$Woman.Councilors.Share.AllParties <- councilors.gendershare.total$Woman
candidates.gendershare.total$Year <- candidates.gendershare.total$ANO_ELEICAO - 4
candidates.gendershare.total$Woman.CouncilCandidatesShare.AllParties <- candidates.gendershare.total$Woman
data <- merge(data, councilors.gendershare.total[, c("MunCode", "Year", "Woman.Councilors.Share.AllParties")], all.x=T)
data <- merge(data, candidates.gendershare.total[, c("MunCode", "Year", "Woman.CouncilCandidatesShare.AllParties")], all.x=T)

# Add data on campaign donations in the next election (this will be used in Appendix TABLE P.1)
donations.data <- readRDS("../1_data/raw/data_raw_CampaignDonationsCouncil.rds")
donations.data$Female.Candidate <- donations.data$Sex == "FEMININO"
donations.data$Male.Candidate <- donations.data$Sex == "MASCULINO"
donations.data$Female.Donations <- ifelse(donations.data$Female.Candidate, donations.data$AllDonations, 0)
donations.data$Male.Donations <- ifelse(donations.data$Male.Candidate, donations.data$AllDonations, 0)
aggregated.donations <- aggregate(cbind(Female.Donations, Male.Donations, Female.Candidate, Male.Candidate) ~ MunCode + ANO_ELEICAO, FUN="sum", data=donations.data)
aggregated.donations$Female.Donations.Ratio <- (aggregated.donations$Female.Donations / aggregated.donations$Female.Candidate) / (aggregated.donations$Male.Donations / aggregated.donations$Male.Candidate)
# Adjust the dates so that they are matched to the previous municipal election
aggregated.donations$Year <- aggregated.donations$ANO_ELEICAO - 4
data <- merge(data, aggregated.donations[, c("MunCode", "Year", "Female.Donations.Ratio")], all.x=T, by=c("MunCode", "Year"))

# Store for later use in the RDD dataset
recruits <- data







### Step 2: Code running variable

# Load municipal election data
elections <- readRDS("../1_data/raw/data_raw_MunicipalElections.rds")

# Extract first-round mayoral elections
mayoral <- elections[elections$DESCRICAO_CARGO %in% c("PREFEITO", "Prefeito") & elections$NUM_TURNO == 1,]

# Remove municipalities where the election went to a second round
second.round.muns <- mayoral$CODIGO_MUNICIPIO[mayoral$DESC_SIT_CAND_TOT == "2º TURNO"]
mayoral <- mayoral[!(mayoral$CODIGO_MUNICIPIO %in% second.round.muns),]

# Remove candidates who were substituted or died before the election
mayoral <- mayoral[!(mayoral$DESC_SIT_CAND_TOT %in% c("SUBSTITUÍDO", "RENÚNCIA/FALECIMENTO COM SUBSTITUIÇÃO")),]

# aggregate over all electoral precincts in the municipality
mayoral <- aggregate(TOTAL_VOTOS ~ ANO_ELEICAO + CODIGO_MUNICIPIO + NOME_MUNICIPIO + SIGLA_UF + NOME_CANDIDATO + NUMERO_CAND + SQ_CANDIDATO  + SIGLA_PARTIDO + DESC_SIT_CAND_TOT, FUN="sum", data=mayoral)

names(mayoral)[names(mayoral) == "TOTAL_VOTOS"] <- "Votes"


# Count all of the votes cast for mayoral candidates in the municipality
all.votes <- aggregate(Votes ~ ANO_ELEICAO + CODIGO_MUNICIPIO, FUN="sum", data=mayoral)
names(all.votes)[3] <- "Total.Votes"

# Calculate voteshares
mayoral <- merge(mayoral, all.votes, by=c("ANO_ELEICAO", "CODIGO_MUNICIPIO"))
mayoral$Voteshare <- mayoral$Votes / mayoral$Total.Votes

# Remove the cases where there is a discrepancy between the candidate identified as the "winner"
# and the candidate who won the most votes. Most of these cases are elections where one of the candidates
# was invalidated after the fact, and the election had to be held again later
winners.votes <- aggregate(Voteshare ~ ANO_ELEICAO + CODIGO_MUNICIPIO, data=mayoral, FUN="max")
winners <- merge(mayoral, winners.votes)
mismatched <- winners[winners$DESC_SIT_CAND_TOT != "ELEITO",]
mismatched.mun.year <- paste(mismatched$CODIGO_MUNICIPIO, mismatched$ANO_ELEICAO)
mayoral <- mayoral[!(paste(mayoral$CODIGO_MUNICIPIO, mayoral$ANO_ELEICAO) %in% mismatched.mun.year),]

# Find top two candidates
# Because these were FPTP elections, the candidate who won is the candidate with the highest voteshare
mayoral$Winner <- mayoral$DESC_SIT_CAND_TOT == "ELEITO"
winners <- mayoral[mayoral$Winner,]

# The runner-up is the candidate with the most votes among the candidates who didn't win
runners.up <- aggregate(Voteshare ~ ANO_ELEICAO + CODIGO_MUNICIPIO +SIGLA_UF, FUN="max", data=mayoral[!mayoral$Winner,])
runners.up <- merge(runners.up, mayoral)

top2 <- rbind(winners, runners.up)


# Remove the cases where the winner ran unopposed, and the cases where there were either
# multiple candidates were identified as having won (most of these are cases where an election was later invalidated)
# and the cases where there were exact ties for second-place
two.candidates <- names(table(paste(top2$CODIGO_MUNICIPIO, top2$ANO_ELEICAO)))[table(paste(top2$CODIGO_MUNICIPIO, top2$ANO_ELEICAO)) == 2]
top2 <- top2[paste(top2$CODIGO_MUNICIPIO, top2$ANO_ELEICAO) %in% two.candidates,]
top2$MunCode <-  str_remove(top2$CODIGO_MUNICIPIO, "^0+") # Remove leading zeroes from municipality code to match membership data

# Merge in data on the candidates' gender
candidates <- readRDS("../1_data/cleaned/data_cleaned_Candidates.rds")
candidates <- candidates[candidates$DESCRICAO_CARGO == "PREFEITO",]
top2$SEQUENCIAL_CANDIDATO <- top2$SQ_CANDIDATO
merged.data <- merge(top2[, !(names(top2) %in% c("NOME_CANDIDATO"))], candidates[, !(names(candidates) %in% c("SIGLA_PARTIDO", "Winner"))], by=c("SEQUENCIAL_CANDIDATO", "ANO_ELEICAO", "MunCode"), all.x=T)

# Identify the races where the top 2 candidates were 1 man and 1 woman,
merged.data$Female.Candidate <- as.numeric(merged.data$DESCRICAO_SEXO == "FEMININO")
merged.data$Male.Candidate <- as.numeric(merged.data$DESCRICAO_SEXO == "MASCULINO")
by.municipality.year <- aggregate(cbind(Female.Candidate, Male.Candidate) ~ CODIGO_MUNICIPIO + ANO_ELEICAO, FUN="sum", data=merged.data)
by.municipality.year <- by.municipality.year[by.municipality.year$Female.Candidate == 1 & by.municipality.year$Male.Candidate == 1,]
onewoman.oneman <- merge(merged.data, by.municipality.year[, c("CODIGO_MUNICIPIO", "ANO_ELEICAO")])

unwanted_array = list(    'Š'='S', 'š'='s', 'Ž'='Z', 'ž'='z', 'À'='A', 'Á'='A', 'Â'='A', 'Ã'='A', 'Ä'='A', 'Å'='A', 'Æ'='A', 'Ç'='C', 'È'='E', 'É'='E',
                          'Ê'='E', 'Ë'='E', 'Ì'='I', 'Í'='I', 'Î'='I', 'Ï'='I',  'Ò'='O', 'Ó'='O', 'Ô'='O', 'Õ'='O', 'Ö'='O', 'Ø'='O', 'Ù'='U',
                          'Ú'='U', 'Û'='U', 'Ü'='U', 'Ý'='Y', 'Þ'='B', 'ß'='Ss', 'à'='a', 'á'='a', 'â'='a', 'ã'='a', 'ä'='a', 'å'='a', 'æ'='a', 'ç'='c',
                          'è'='e', 'é'='e', 'ê'='e', 'ë'='e', 'ì'='i', 'í'='i', 'î'='i', 'ï'='i', 'ð'='o', 'ò'='o', 'ó'='o', 'ô'='o', 'õ'='o',
                          'ö'='o', 'ø'='o', 'ù'='u', 'ú'='u', 'û'='u', 'ý'='y', 'ý'='y', 'þ'='b', 'ÿ'='y' )
onewoman.oneman$NOME_CANDIDATO <- chartr(paste(names(unwanted_array), collapse=''),
                                         paste(unwanted_array, collapse=''),
                                         onewoman.oneman$NOME_CANDIDATO)

# Code female candidate's margin of victory
woman <- onewoman.oneman
woman$Female.Voteshare <- ifelse(woman$Female.Candidate == 1, woman$Voteshare, 0)
woman$Male.Voteshare <- ifelse(woman$Male.Candidate == 1, woman$Voteshare, 0)
by.municipality.year <- aggregate(cbind(Female.Voteshare, Male.Voteshare) ~ CODIGO_MUNICIPIO + ANO_ELEICAO, FUN="sum", data=woman)
by.municipality.year$Margin <- by.municipality.year$Female.Voteshare - by.municipality.year$Male.Voteshare
woman <- merge(woman[ , !(names(woman) %in% c("Female.Voteshare", "Male.Voteshare"))], by.municipality.year[, c("CODIGO_MUNICIPIO", "ANO_ELEICAO", "Female.Voteshare", "Male.Voteshare", "Margin")])
woman <- woman[woman$Female.Candidate == 1,]
recruits$ANO_ELEICAO <- recruits$PERIODO
woman <- merge(woman, recruits, by=c("ANO_ELEICAO", "MunCode", "Party"), all.x=T)
var.names <- c("Recruit.Share", "Female.Recruit.Share", "Male.Recruit.Share", "Attrition.Rate", "Attrition.Rate.Female", "Attrition.Rate.Male", "Gender.Gap", "Gender.Gap.Year1", "Gender.Gap.Year2", "Gender.Gap.Year3", "Gender.Gap.Year4")
woman[, var.names][is.na(woman[, var.names])] <- 0
# Note: In general, missing values on these variables are zeroes (if no members were recruited in the municipality during that term, then that party-municipality-term observation will not appear in the data_cleaned_filiados_ files).
# However, membership data was not available for the Brazilian Socialist Party (PSB), and it should be coded as missing:
woman[woman$Party %in% c("PSB"), var.names] <- NA
woman <- woman[!is.na(woman$Party),]
woman$Type <- "Woman vs. Man"


# Same thing, but now for male candidates
man <- onewoman.oneman
man$Female.Voteshare <- ifelse(man$Female.Candidate == 1, man$Voteshare, 0)
man$Male.Voteshare <- ifelse(man$Male.Candidate == 1, man$Voteshare, 0)
by.municipality.year <- aggregate(cbind(Female.Voteshare, Male.Voteshare) ~ CODIGO_MUNICIPIO + ANO_ELEICAO, FUN="sum", data=man)
by.municipality.year$Margin <- by.municipality.year$Male.Voteshare - by.municipality.year$Female.Voteshare
man <- merge(man, by.municipality.year[, c("CODIGO_MUNICIPIO", "ANO_ELEICAO", "Margin")])
man <- man[man$Female.Candidate == 0,]
man <- merge(man, recruits, by=c("ANO_ELEICAO", "MunCode", "Party"), all.x=T)
man[, var.names][is.na(man[, var.names])] <- 0
man[man$Party %in% c("PSB"), var.names] <- NA
man <- man[!is.na(man$Party),]
man$Type <- "Man vs. Woman"


# Merge in additional candidate data
candidates.data <- readRDS("../1_data/raw/data_raw_MayoralCandidatesData.rds")
woman$CPF_CANDIDATO <- as.numeric(woman$CPF_CANDIDATO)
man$CPF_CANDIDATO <- as.numeric(man$CPF_CANDIDATO)
candidates.data$CPF_CANDIDATO <- suppressWarnings(as.numeric(candidates.data$CPF_CANDIDATO)) 
woman <- merge(woman, candidates.data, by=c("ANO_ELEICAO", "CPF_CANDIDATO",  "NOME_CANDIDATO", "CODIGO_MUNICIPIO"))
man <- merge(man, candidates.data, by=c("ANO_ELEICAO",  "CPF_CANDIDATO",  "NOME_CANDIDATO", "CODIGO_MUNICIPIO"))


# Code data on the candidate's challenger
male.challengers <- onewoman.oneman[onewoman.oneman$Female.Candidate == 0, c("CODIGO_MUNICIPIO", "ANO_ELEICAO", "NOME_CANDIDATO", "SIGLA_PARTIDO")]
female.challengers <- onewoman.oneman[onewoman.oneman$Female.Candidate == 1, c("CODIGO_MUNICIPIO", "ANO_ELEICAO", "NOME_CANDIDATO", "SIGLA_PARTIDO")]
male.challengers <- merge(male.challengers, candidates.data[, c("ANO_ELEICAO", "CODIGO_MUNICIPIO", "NOME_CANDIDATO", "Dynastic", "Incumbent", "Prior.Office", "Time.In.Politics", "University")], by=c("ANO_ELEICAO", "CODIGO_MUNICIPIO", "NOME_CANDIDATO"))
female.challengers <- merge(female.challengers, candidates.data[, c("ANO_ELEICAO", "CODIGO_MUNICIPIO", "NOME_CANDIDATO", "Dynastic", "Incumbent", "Prior.Office", "Time.In.Politics", "University")], by=c("ANO_ELEICAO", "CODIGO_MUNICIPIO", "NOME_CANDIDATO"))
names(male.challengers) <- c("ANO_ELEICAO", "CODIGO_MUNICIPIO", "Challenger_Name", "Challenger_Party", "Challenger_Dynastic", "Challenger_Incumbent", "Challenger_Prior.Office", "Challenger_Time.In.Politics", "Challenger_University")
names(female.challengers) <- c("ANO_ELEICAO", "CODIGO_MUNICIPIO", "Challenger_Name", "Challenger_Party", "Challenger_Dynastic", "Challenger_Incumbent", "Challenger_Prior.Office", "Challenger_Time.In.Politics", "Challenger_University")
woman <- merge(woman, male.challengers, by=c("CODIGO_MUNICIPIO", "ANO_ELEICAO"), all.x=T)
man <- merge(man, female.challengers, by=c("CODIGO_MUNICIPIO", "ANO_ELEICAO"), all.x=T)

# Remove the variables that we won't need again
woman$Year <- woman$ANO_ELEICAO
man$Year <- man$ANO_ELEICAO
save.vars <- c("Year", "MunCode", "Party", "SIGLA_PARTIDO", "NOME_CANDIDATO", "Voteshare", "Winner", "Woman", "Margin", "Voters", "Female.Voters", "Male.Voters", "Recruit.Share", "Gender.Gap", "Attrition.Rate", "Attrition.Rate.Female", "Attrition.Rate.Male", "Switch.Rate.Female", "Switch.Rate.Male", "Renominated", "Advanced", "Cumulative.Female.Recruits.Share", "Recruit.Share.Lagged", "Gender.Gap.Lagged", "Attrition.Rate.Lagged", "University", "Incumbent", "Married", "Age", "Dynastic", "Insider", "Time.In.Party", "Time.In.Politics", "Prior.Office", "First.Female.Mayor", "Prior.Female.Mayors", "Challenger_Incumbent", "Challenger_Time.In.Politics", "Illiteracy_2000", "UrbanizationRate_2000", "HDI_2000", "HDI_2010", "Population_2000", "Female.Participation.Rate", "Voter.Gender.Ratio", "PT.Deputies.Voteshare", "Gender.Gap.Year1", "Gender.Gap.Year2", "Gender.Gap.Year3", "Gender.Gap.Year4", "Woman.CouncilCandidatesShare.AllParties", "Woman.Councilors.Share.AllParties", "Female.Donations.Ratio")
woman <- woman[, save.vars]
man <- man[, save.vars]



saveRDS(woman, "../1_data/analysis/data_analysis_RDD_women.rds")
saveRDS(man, "../1_data/analysis/data_analysis_RDD_men.rds")
rm( list=ls() )
